package cn.com.homework.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class FileUtil {
	// file operator

	/**
	 * 检查路径合法性
	 */
	public static void checkPath(String path) {
		if (path == null || path.trim().length() == 0) {
			throw new IllegalArgumentException(path + "路径不合法!");
		}
	}

	/**
	 * 根据目录获取文件列表
	 * 
	 * @param hdfsDir
	 * @return
	 */
	public static List<String> getFilePathsByDir(String hdfsDir,
			Configuration conf) {
		// 1.合法性检验
		checkPath(hdfsDir);
		// 2.集合
		List<String> result = new ArrayList<String>();

		FileSystem fs = null;
		try {
			fs = FileSystem.get(conf);
			FileStatus[] files = fs.listStatus(new Path(hdfsDir));
			for (FileStatus file : files) {
				result.add(file.toString());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}
}
